package gui;

import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.PaintEvent;

public class Graphs extends Dialog {

	private static ArrayList<Double> fpsData;
	private static int fpsIncrement;
	private static int counter = 0; //for graph refreshing
	
	protected Object result;
	protected Shell shell;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public Graphs(Shell parent, int style, ArrayList<Double> initFPSData, int initFPSIncrement) {
		super(parent, style);
		setText("SWT Dialog");
		fpsData = initFPSData;
		fpsIncrement = initFPSIncrement;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(700, 300);
		shell.setText(getText());
		shell.setLayout(null);
		
		Composite composite = new Composite(shell, SWT.BORDER);
		composite.setBounds(10, 10, 220, 254);
		composite.setLayout(null);
		
		Button btnFps = new Button(composite, SWT.CHECK);
		btnFps.setBounds(10, 10, 93, 16);
		btnFps.setText("FPS");
		
		final Canvas canvas = new Canvas(composite, SWT.BORDER);
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				//if(counter % fpsIncrement == 0){
				canvas.redraw();
				System.out.println(canvas.getBorderWidth());
				graphPoints(fpsData, fpsIncrement, e, canvas.getBorderWidth());
				//}
				counter++;
			}
		});
		canvas.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		canvas.setBounds(0, 32, 216, 218);
		
		Composite composite_1 = new Composite(shell, SWT.BORDER);
		composite_1.setLayout(null);
		composite_1.setBounds(236, 10, 220, 254);
		
		Button btnBoidCount = new Button(composite_1, SWT.CHECK);
		btnBoidCount.setText("Boid Count\r\n");
		btnBoidCount.setBounds(10, 10, 93, 16);
		
		Canvas canvas_1 = new Canvas(composite_1, SWT.BORDER);
		canvas_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		canvas_1.setBounds(0, 32, 216, 218);
		
		Composite composite_2 = new Composite(shell, SWT.BORDER);
		composite_2.setLayout(null);
		composite_2.setBounds(462, 10, 220, 254);
		
		Button btnBoidFrequency = new Button(composite_2, SWT.CHECK);
		btnBoidFrequency.setText("Boid Frequency\r\n");
		btnBoidFrequency.setBounds(10, 10, 169, 16);
		
		Canvas canvas_2 = new Canvas(composite_2, SWT.BORDER);
		canvas_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		canvas_2.setBounds(0, 32, 216, 218);			
	}
	
	public static void graphPoints(ArrayList<Double> points, int increment, PaintEvent e, int width){
		int dataSize = points.size();
		for(int i = width; i > 1; i -= increment){
			e.gc.drawLine(i, points.get((int) (dataSize - (i * increment))).intValue(), i - increment, points.get((int) (dataSize - (i * increment) - 1)).intValue());
		}
	}
	
	public static void updateFPSData(ArrayList<Double> newFPSData, int newIncrement){
		fpsData = newFPSData;
		fpsIncrement = newIncrement;
	}
}
